﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Entity = SoTaiChinh.Library.Entity;
using Business = SoTaiChinh.Library.Business;
using Common = SoTaiChinh.Library.Common.Algorithm;
public partial class controls_admin_addmenu : System.Web.UI.UserControl
{
    Common.Validators validator = new Common.Validators();

    private static DataTable dsDanhMuc = new DataTable();
    DataTable dsTinTuc = new DataTable();
    DataTable dsBinhLuan = new DataTable();

    Entity.DanhMuc entityDanhMuc = new Entity.DanhMuc();
    Entity.DanhMuc entityDanhMucCon1 = new Entity.DanhMuc();
    Entity.DanhMuc entityDanhMucCon2 = new Entity.DanhMuc();
    Entity.DanhMuc entity = new Entity.DanhMuc();
    Entity.TinTuc entityTintuc = new Entity.TinTuc();
    Entity.BinhLuan entityBinhLuan = new Entity.BinhLuan();

    Business.DanhMuc business = new Business.DanhMuc();
    Business.TinTuc businessTinTuc = new Business.TinTuc();
    Business.BinhLuan businessBinhLuan = new Business.BinhLuan();

    protected void Page_Load(object sender, EventArgs e)
    { 
        if (!IsPostBack)
        {
            LoadDanhMuc();
            string action = Request.QueryString["action"].ToString();
            //Nếu là thêm mới danh mục
            if (action == "add")
            {
                lbTitle.Text = "Thêm danh mục";
            }
            //Nếu là sửa
            if (action == "edit")
            {
                lbTitle.Text = "Sửa danh mục";
                int info = Convert.ToInt32(Request.QueryString["info"].ToString());
                entityDanhMuc.Id = info;
                DataTable dtDanhMuc = business.SelectPrimaryKey(entityDanhMuc);
                txtTenDanhMuc.Text = dtDanhMuc.Rows[0]["Ten"].ToString();
                ddlDanhMuc.SelectedValue = dtDanhMuc.Rows[0]["DanhMucCha"].ToString();
                cbTrangThai.Checked = Convert.ToBoolean(dtDanhMuc.Rows[0]["TrangThai"].ToString());
                ddlTinTuc.SelectedValue = dtDanhMuc.Rows[0]["TinTuc"].ToString();
                ddlTren.SelectedValue = dtDanhMuc.Rows[0]["Tren"].ToString();
                ddlPhai.SelectedValue = dtDanhMuc.Rows[0]["Phai"].ToString();
                ddlDuoi.SelectedValue = dtDanhMuc.Rows[0]["Duoi"].ToString();
                ddlTrai.SelectedValue = dtDanhMuc.Rows[0]["Trai"].ToString();
            }
            //Nếu là xóa
            if (action == "delete")
            {
                int id = Convert.ToInt32(Request.QueryString["info"].ToString()) ;
                entityDanhMuc.Id = id;
                entityDanhMucCon1.DanhMucCha = id;
                DataTable dt1 = business.SelectByDanhMucCha(entityDanhMucCon1);
                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        entityDanhMucCon2.DanhMucCha = Convert.ToInt32(dt1.Rows[i]["Id"].ToString());
                        DataTable dt2 = business.SelectByDanhMucCha(entityDanhMucCon2);
                        if (dt2.Rows.Count > 0)
                        {
                            for (int j = 0; j < dt2.Rows.Count; j++)
                            {
                                //entityDanhMucCon2.Id = Convert.ToInt32(dt2.Rows[j]["Id"].ToString());
                                //business.Delete(entityDanhMucCon2);
                                int id3 = Convert.ToInt32(dt2.Rows[j]["Id"].ToString());
                                DeleteFK(id3);
                            }
                            //entityDanhMucCon1.Id = Convert.ToInt32(dt1.Rows[i]["Id"].ToString());
                            //business.Delete(entityDanhMucCon1);
                            int id2 = Convert.ToInt32(dt1.Rows[i]["Id"].ToString());
                            DeleteFK(id2);
                        }
                        else
                        {
                            //entityDanhMucCon1.Id = Convert.ToInt32(dt1.Rows[i]["Id"].ToString());
                            //business.Delete(entityDanhMucCon1);
                            int id1 = Convert.ToInt32(dt1.Rows[i]["Id"].ToString());
                            DeleteFK(id1);
                        }
                    }
                    //business.Delete(entityDanhMuc);
                    DeleteFK(id);
                }
                else
                {
                    //business.Delete(entityDanhMuc); 
                    DeleteFK(id);
                }
                
                Response.Redirect("~/admin/listmenu.aspx");
            }
        }
    }

    // Ham xoa khoa ngoai
    protected void DeleteFK(int id)
    {
        try
        {
            entityTintuc.ChuyenMuc = id;
            dsTinTuc = businessTinTuc.SelectByDanhMuc(entityTintuc);
            if (dsTinTuc.Rows.Count > 0)
            {
                for (int i = 0; i < dsTinTuc.Rows.Count; i++)
                {
                    entityBinhLuan.BaiViet = Convert.ToInt32(dsTinTuc.Rows[i]["Id"].ToString());
                    dsBinhLuan = businessBinhLuan.SelectTheoBaiViet(entityBinhLuan);
                    if (dsBinhLuan.Rows.Count > 0)
                    {
                        for (int j = 0; j < dsBinhLuan.Rows.Count; j++)
                        {
                            entityBinhLuan.Id = Convert.ToInt32(dsBinhLuan.Rows[j]["Id"].ToString());
                            businessBinhLuan.Delete(entityBinhLuan);
                        }
                        entityTintuc.Id = Convert.ToInt32(dsTinTuc.Rows[i]["Id"].ToString());
                        businessTinTuc.Delete(entityTintuc);
                    }
                    else
                    {
                        entityTintuc.Id = Convert.ToInt32(dsTinTuc.Rows[i]["Id"].ToString());
                        businessTinTuc.Delete(entityTintuc);
                    }
                }
                entity.Id = id;
                business.Delete(entity);
            }
            else
            {
                entity.Id = id;
                business.Delete(entity);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    //Hàm load ddlDanhMuc
    protected void LoadDanhMuc()
    {
        try
        {
            dsDanhMuc = business.Select();
            foreach (DataRow dr in dsDanhMuc.Rows)
            {
                if (dr["DanhMucCha"].ToString() == "0")
                {
                    ListItem item = new ListItem();
                    item.Text = dr["Ten"].ToString();
                    item.Value = dr["Id"].ToString();
                    ddlDanhMuc.Items.Add(item);
                    foreach (DataRow drr in dsDanhMuc.Rows)
                    {
                        string child1 = "----- ";
                        if (drr["DanhMucCha"].ToString() == dr["Id"].ToString())
                        {
                            ListItem itemChild1 = new ListItem();
                            itemChild1.Text = child1 + drr["Ten"].ToString();
                            itemChild1.Value = drr["Id"].ToString();
                            ddlDanhMuc.Items.Add(itemChild1);
                            foreach (DataRow drrr in dsDanhMuc.Rows)
                            {
                                string child2 = "------------ ";
                                if (drrr["DanhMucCha"].ToString() == drr["Id"].ToString())
                                {
                                    ListItem itemChild2 = new ListItem();
                                    itemChild2.Text = child2 + drrr["Ten"].ToString();
                                    itemChild2.Value = drrr["Id"].ToString();
                                    ddlDanhMuc.Items.Add(itemChild2);
                                }
                            }
                        }
                    }
                }
            }
            ListItem itemRoot = new ListItem();
            itemRoot.Text = "--- Danh mục gốc ---";
            itemRoot.Value = "0";
            ddlDanhMuc.Items.Insert(0, itemRoot);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    //Hàm quay lại trang danh sách danh mục
    protected void btnQuayLai_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/listmenu.aspx");
    }
    //Hàm kiểm tra danh mục thêm vào hợp lệ hay không
    protected bool KiemTraDanhMucHopLe()
    {
        bool kt = false;
        if (Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString()) != 0)
        {
            int id = Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString());
            Entity.DanhMuc entity = new Entity.DanhMuc();
            entity.Id = id;
            DataTable dt = business.SelectPrimaryKey(entity);
            if (Convert.ToInt32(dt.Rows[0]["Cap"].ToString()) != 3)
            {
                kt = true;
            }
            else
            {
                kt = false;
            }
        }
        else
        {
            kt = true;
        }
        return kt;
    }
    //Hàm xử lý button Lưu
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        try
        {
            string action = Request.QueryString["action"].ToString();
            int info = Convert.ToInt32(Request.QueryString["info"].ToString());
            //Nếu là sửa
            if (action == "edit")
            {
                if (KiemTraDanhMucHopLe() == true)
                {
                    bool save = false;
                    entityDanhMuc.Id = info;
                    entityDanhMuc.Ten = txtTenDanhMuc.Text;
                    entityDanhMuc.DanhMucCha = Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString());
                    entityDanhMuc.TrangThai = cbTrangThai.Checked;
                    if (Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString()) == 0)
                    {
                        entityDanhMuc.Cap = 1;
                    }
                    else
                    {
                        Entity.DanhMuc entityTemp = new Entity.DanhMuc();
                        entityTemp.Id = Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString());
                        DataTable dt = business.SelectPrimaryKey(entityTemp);
                        entityDanhMuc.Cap = Convert.ToInt32(dt.Rows[0]["Cap"].ToString()) + 1;
                    }
                    entityDanhMuc.TinTuc = Convert.ToBoolean(ddlTinTuc.SelectedValue.ToString());
                    entityDanhMuc.Tren = Convert.ToBoolean(ddlTren.SelectedValue.ToString());
                    entityDanhMuc.Phai = Convert.ToBoolean(ddlPhai.SelectedValue.ToString());
                    entityDanhMuc.Duoi = Convert.ToBoolean(ddlDuoi.SelectedValue.ToString());
                    entityDanhMuc.Trai = Convert.ToBoolean(ddlTrai.SelectedValue.ToString());
                    save = business.Update(entityDanhMuc);
                    if (save == true)
                    {
                        Response.Redirect(Request.Url.ToString());
                        DataTable dtDanhMuc = business.SelectPrimaryKey(entityDanhMuc);
                        ddlDanhMuc.SelectedValue = dtDanhMuc.Rows[0]["DanhMucCha"].ToString();
                        msgSuccess.Style.Add("display", "block");
                        msgError.Style.Add("display", "none");
                    }
                    else
                    {
                        msgError.Style.Add("display", "block");
                        msgSuccess.Style.Add("display", "none");
                        msgErrorDdlDanhMuc.Style.Add("display", "none");
                        return;
                    }
                }
                else
                {
                    msgErrorDdlDanhMuc.Style.Add("display", "block");
                    return;
                }             
            }
            //Nếu là thêm
            if (action == "add")
            {
                if (KiemTraDanhMucHopLe() == true)
                {
                    bool save = false;
                    if (Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString()) != 0)
                    {
                        entityDanhMuc.Id = Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString());
                        DataTable dt = business.SelectPrimaryKey(entityDanhMuc);
                        entityDanhMuc.Cap = Convert.ToInt32(dt.Rows[0]["Cap"].ToString()) + 1;
                    }
                    else
                    {
                        entityDanhMuc.Cap = 1;
                    }
                    entityDanhMuc.DanhMucCha = Convert.ToInt32(ddlDanhMuc.SelectedValue.ToString());
                    entityDanhMuc.Ten = txtTenDanhMuc.Text;
                    entityDanhMuc.SapXep = 1;
                    entityDanhMuc.TinTuc = Convert.ToBoolean(ddlTinTuc.SelectedValue.ToString());
                    entityDanhMuc.Tren = Convert.ToBoolean(ddlTren.SelectedValue.ToString());
                    entityDanhMuc.Phai = Convert.ToBoolean(ddlPhai.SelectedValue.ToString());
                    entityDanhMuc.Duoi = Convert.ToBoolean(ddlDuoi.SelectedValue.ToString());
                    entityDanhMuc.Trai = Convert.ToBoolean(ddlTrai.SelectedValue.ToString());
                    entityDanhMuc.TrangThai = cbTrangThai.Checked;
                    save = business.Insert(entityDanhMuc);
                    if (save == true)
                    {
                        Response.Redirect(Request.Url.ToString());
                    }
                    else
                    {
                        msgError.Style.Add("display", "block");
                        msgSuccess.Style.Add("display", "none");
                        msgErrorDdlDanhMuc.Style.Add("display", "none");
                        return;
                    }
                }
                else
                {
                    msgErrorDdlDanhMuc.Style.Add("display", "block");
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }    
}
